package com.fxkj.cloud.drms.infra.repository;

import com.fxkj.cloud.dbs.common.model.DeviceBasicInfo;
import com.fxkj.cloud.dbs.common.model.attribute.AttributeConstants;
import com.fxkj.cloud.dbs.common.model.kv.KvEntry;
import com.fxkj.cloud.dbs.common.model.product.RelProductAttributeInfo;
import com.fxkj.cloud.drms.domain.device.repository.DeviceRepository;
import com.fxkj.cloud.drms.domain.product.ProductEnergyStatistics;
import com.fxkj.cloud.drms.domain.product.ProductRepository;
import com.fxkj.cloud.drms.domain.ts.repository.TimeseriesRepository;
import com.fxkj.cloud.drms.infra.persistence.cache.ProductCacheRepository;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.MoreExecutors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>description: </p>
 * @author ww
 * @date 2025/4/17 10:04
 */
@Component
@RequiredArgsConstructor
public class ProductRepositoryImpl implements ProductRepository {
    private final ProductCacheRepository productCacheRepository;
    private final DeviceRepository deviceRepository;
    private final TimeseriesRepository timeseriesRepository;

    @Override
    public RelProductAttributeInfo getAlarmAttributeName(UUID productId, String attributeCode) {
        return productCacheRepository.getAlarmAttributeInfo(productId, attributeCode);
    }

    @Override
    public ListenableFuture<ProductEnergyStatistics> getProductEnergyStatistics(Set<UUID> productIds, UUID organizationId) {
        // 执行设备能耗统计异步任务
        List<DeviceBasicInfo> deviceBasicInfos = deviceRepository.getAllDeviceBasicInfo(productIds);
        Map<UUID, Float> deviceEnergyMap = deviceBasicInfos.stream().collect(Collectors.toMap(DeviceBasicInfo::getDeviceId, DeviceBasicInfo::getRateEnergy));

        List<String> queryAttributeKeys = Arrays.asList(
                //总工作时长
                AttributeConstants.Service.S.Code.CT.getFullCode(),
                //昨日工作时长
                AttributeConstants.Service.S.Code.LDH.getFullCode(),
                //今日工作时长
                AttributeConstants.Service.S.Code.CDH.getFullCode(),
                //上月工作时长
                AttributeConstants.Service.S.Code.LMH.getFullCode(),
                //本月工作时长
                AttributeConstants.Service.S.Code.CMH.getFullCode(),
                //总工作时长
                AttributeConstants.Service.S.Code.WH.getFullCode(),
                //今日能耗
                AttributeConstants.Service.E.Code.CDE.getFullCode(),
                //昨日能耗
                AttributeConstants.Service.E.Code.LDE.getFullCode(),
                //本月能耗
                AttributeConstants.Service.E.Code.CME.getFullCode(),
                //上月能耗
                AttributeConstants.Service.E.Code.LME.getFullCode(),
                //总能耗
                AttributeConstants.Service.E.Code.E.getFullCode()
        );
        ListenableFuture<Map<UUID, Map<String, KvEntry>>> devicesLastValueMap = timeseriesRepository.queryDevicesLastValueByProductId(productIds, queryAttributeKeys,organizationId);
        return Futures.transform(devicesLastValueMap, input -> ProductEnergyStatistics.statistics(deviceEnergyMap, input), MoreExecutors.directExecutor());
    }

    @Override
    public Set<UUID> getProductIdsByModelId(UUID productModelId) {
        return productCacheRepository.getProductIdsByModelId(productModelId);
    }
}
